package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcLifeActionOldmanDto;
import com.csun.cmny.provider.dto.OmcLifeOldmanQueryDto;
import com.csun.cmny.provider.model.dto.FollowDataPageQueryOld;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcLifeActionOldmanService;
import com.csun.cmny.provider.service.OmcNursingRoomBindService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lijun
 * @description:
 * @date 2019/12/31 9:52
 */
@RestController
@RequestMapping(value = "/lifeOldman", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcLifeOldmanController extends BaseController {
    @Resource
    private OmcLifeActionOldmanService omcLifeActionOldmanService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcNursingRoomBindService omcNursingRoomBindService;

    /**
     * 添加入居者
     *
     * @param omcLifeActionOldmanDto
     *
     * @return
     */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/addLifeOldman")
    public Wrapper addLifeOldman(@RequestBody OmcLifeActionOldmanDto omcLifeActionOldmanDto){
        Integer result = omcLifeActionOldmanService.addLifeOldman(omcLifeActionOldmanDto,getLoginAuthDto());
        if (result < 1) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"添加入居者失败", null);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), null);
        }
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 删除入居者
     *
     * @param oldmanId
     *
     * @return  the wrapper.
     */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/deleteById/{oldmanId}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long oldmanId) {
        if(oldmanId == null || oldmanId < 1){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, null);
        }
        Integer result = omcLifeActionOldmanService.deleteById(oldmanId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, null);
        }
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询入居者
     * @param omcLifeOldmanQueryDto
     * @return
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryLifeOldmanWithPage", method = RequestMethod.POST)
    public Wrapper queryLifeOldmanWithPage ( OmcLifeOldmanQueryDto omcLifeOldmanQueryDto){
        omcLifeOldmanQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcLifeActionOldmanService.queryLifeOldmanWithPage(omcLifeOldmanQueryDto);
//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 修改入居者详情
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/updateById")
    public Wrapper updateById(@RequestBody OmcLifeActionOldmanDto omcLifeActionOldmanDto) {
        if(omcLifeActionOldmanDto.getOldmanId() == null ||omcLifeActionOldmanDto.getOldmanId().equals(0L)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,null);
        }
        Integer result = omcLifeActionOldmanService.updateById(omcLifeActionOldmanDto,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, null);
        }
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    //查一个班组内所有的老人id
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/selectOldIdByNuringId")
    public Wrapper selectOldIdByNuringId(FollowDataPageQueryOld followDataPageQueryOld){
        followDataPageQueryOld.setGroupId(getLoginAuthDto().getGroupId());
        List<Long>oldIds= omcNursingRoomBindService.selectTemperatureByNuringId(followDataPageQueryOld);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), oldIds);
    }



}
